<template>
  <el-dialog :title="title" width="30%" draggable
              :close-on-click-modal="false" v-model="visible" @open="submitted = false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit(dataFormRef)" label-width="80px">
      <el-form-item v-if="type === 1" label="属性分组" prop="categoryId">
        <el-select style="width: 100%" v-model="dataForm.attrgroupId" filterable placeholder="请选择属性分组">
          <el-option v-for="item in attrGroupList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="属性名" prop="name">
        <el-input v-model="dataForm.name" minlength="2" maxlength="20" show-word-limit placeholder="属性名"></el-input>
      </el-form-item>
      <el-form-item label="可选值" prop="options">
        <el-select style="width: 100%; margin-bottom: 0px" v-model="dataForm.options" multiple filterable allow-create placeholder="请输入内容"></el-select>
      </el-form-item>
      <el-form-item label="启用多选">
        <el-col :span="8">
          <el-switch v-model="dataForm.multiple" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
        </el-col>
        <el-col :span="6">
          <label>快速展示</label>
        </el-col>
        <el-col :span="8">
          <el-switch v-model="dataForm.visible" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
        </el-col>
      </el-form-item>
      <el-form-item label="启用状态">
        <el-col :span="8">
          <el-switch v-model="dataForm.enable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
        </el-col>
        <el-col :span="6">
          <label>是否检索</label>
        </el-col>
        <el-col :span="8">
          <el-switch v-model="dataForm.searchable" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
        </el-col>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import type { FormInstance } from "element-plus";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";

// 属性定义
const props = defineProps({
  type: {
    type: Number,
    default: 1,
  },
});

// 窗口标题
const title = ref("");

// 弹窗是否显示
const visible = ref(false);

// 提交按钮是否禁用
const submitted = ref(false);

// 表单数据
const dataForm = reactive({
  id: undefined,
  name: "",
  options: "",
  enable: 1,
  visible: 0,
  multiple: 0,
  searchable: 0,
  categoryId: "",
  attrgroupId: "",
});

// 属性分组下拉数据
const attrGroupList = ref([]);

// 表单校验
const dataRule = reactive({
  name: validate.required("属性名"),
  type: validate.required("属性类型"),
  visible: validate.required("快速展示"),
  options: validate.required("属性类型"),
  enable: validate.required("启用状态"),
  searchable: validate.required("是否检索"),
});

// 表单引用
const dataFormRef = ref<FormInstance>();

// 父级组件的自定义事件
const emit = defineEmits(['loadAttrData']);

// 初始化属性分组列表
const initAttrGroupList = (category) => {
  http
    .post({
      url: `/product/attrgroup/list?categoryId=${category.id}`,
    })
    .then(({ data }) => {
      attrGroupList.value = http.success(data) ? data.body : [];
    });
};

// 表单初始化
const initForm = async (action, node, category) => {
  // 初始化属性分组列表
  initAttrGroupList(category);
  // 新增
  if (action === "add") {
    title.value = `新增分类[${category.name}]属性分组`;
    visible.value = true;
    await nextTick();
    dataForm.id = undefined;
    dataForm.name = "";
    dataForm.options = "";
    dataForm.enable = 1;
    dataForm.visible = 0;
    dataForm.multiple = 0;
    dataForm.searchable = 0;
    dataForm.categoryId = category.id;
    return;
  }
  // 修改操作
  dataForm.id = node.id;
  http
    .post({
      url: `/product/attr/info?attrId=${dataForm.id}`,
      method: "post",
    })
    .then(async ({ data }) => {
      if (http.success(data)) {
        title.value = `修改分类[${category.name}]属性分组[${data.body.name}]信息`;
        visible.value = true;
        await nextTick();
        dataFormRef.value.resetFields();
        dataForm.id = data.body.id;
        dataForm.name = data.body.name;
        dataForm.options = data.body.options ? data.body.options.split(";") : [];
        dataForm.enable = data.body.enable;
        dataForm.visible = data.body.visible;
        dataForm.multiple = data.body.multiple;
        dataForm.searchable = data.body.searchable;
        dataForm.attrgroupId = data.body.attrgroupId;
        dataForm.categoryId = category.id;
      }
    });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  form.validate((valid) => {
    if (valid) {
      submitted.value = true;
      http
        .post({
          url: `/product/attr/save`,
          data: {
            id: dataForm.id || undefined,
            type: props.type,
            name: dataForm.name,
            options: dataForm.options.join(";"),
            enable: dataForm.enable,
            visible: dataForm.visible,
            multiple: dataForm.multiple,
            searchable: dataForm.searchable,
            categoryId: dataForm.categoryId,
            attrgroupId: dataForm.attrgroupId,
          },
        }).then(({ data }) => {
          if (http.success(data, true)) {
            visible.value = false;
            emit("loadAttrData");
            return;
          }
          submitted.value = false;
        });
    }
  });
};

// 暴露方法
defineExpose({ initForm });
</script>
